import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;


public class OgrenciTest extends JFrame
{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					OgrenciTest frame = new OgrenciTest();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	public OgrenciTest()
	{
		setTitle("Ogrenci Not Hesaplama Zamazingosu");
		final Ogrenci nOgrenci = new Ogrenci();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 613, 261);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(45dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(22dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(5dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(44dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(22dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(5dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(41dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(22dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(13dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(34dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(45dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblOgrenciAdi = new JLabel("Ogrenci Adi: ");
		contentPane.add(lblOgrenciAdi, "2, 2, right, default");
		final JTextField tfOgrenciAdi = new JTextField();
		contentPane.add(tfOgrenciAdi, "4, 2, 11, 1, fill, default");
		tfOgrenciAdi.setColumns(10);
		
		MaskFormatter ogrNoFormat = null;
		try
		{
			ogrNoFormat = new MaskFormatter("#########");
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		ogrNoFormat.setPlaceholderCharacter('_');
		
		MaskFormatter a = null;
		try
		{
			a = new MaskFormatter("###.##");
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		final MaskFormatter notFormat = a;
		notFormat.setPlaceholderCharacter('0');
		
		JLabel lblOgrenciNo = new JLabel("Ogrenci No: ");
		contentPane.add(lblOgrenciNo, "16, 2, 3, 1, right, default");
		final JFormattedTextField ftfOgrenciNo = new JFormattedTextField(ogrNoFormat);
		contentPane.add(ftfOgrenciNo, "20, 2, fill, default");
		
		JLabel lblVizeNotu = new JLabel("Vize Notu: ");
		contentPane.add(lblVizeNotu, "2, 4, right, default");
		final JFormattedTextField ftfVizeNotu = new JFormattedTextField(notFormat);
		contentPane.add(ftfVizeNotu, "4, 4, fill, default");
		
		JLabel lblFinalNotu = new JLabel("Proje Notu: ");
		contentPane.add(lblFinalNotu, "8, 4, right, default");
		final JFormattedTextField ftfProjeNotu  = new JFormattedTextField(notFormat);
		contentPane.add(ftfProjeNotu, "10, 4, fill, default");
		
		JLabel lblFinalNotu_1 = new JLabel("Final Notu: ");
		contentPane.add(lblFinalNotu_1, "14, 4, right, default");
		final JFormattedTextField ftfFinalNotu = new JFormattedTextField(notFormat);
		contentPane.add(ftfFinalNotu, "16, 4, fill, default");
		
		JLabel lblGenelNot = new JLabel("Genel Not: ");
		contentPane.add(lblGenelNot, "2, 8, right, default");
		
		final JLabel lblGenelNotDegeri = new JLabel("");
		contentPane.add(lblGenelNotDegeri, "4, 8");
		
		JLabel lblHarfNotu = new JLabel("Harf Notu: ");
		contentPane.add(lblHarfNotu, "8, 8, right, default");
		
		final JLabel lblHarfNotuDegeri = new JLabel("");
		contentPane.add(lblHarfNotuDegeri, "10, 8");
		
		JButton btnHesapla = new JButton("Hesapla");
		contentPane.add(btnHesapla, "2, 10, 3, 1");
		btnHesapla.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(Double.parseDouble(ftfVizeNotu.getText()) < 0.0)
					ftfVizeNotu.setText("000.00");
				if(Double.parseDouble(ftfProjeNotu.getText()) < 0.0)
					ftfVizeNotu.setText("000.00");
				if(Double.parseDouble(ftfFinalNotu.getText()) < 0.0)
					ftfVizeNotu.setText("000.00");
				
				if(Double.parseDouble(ftfVizeNotu.getText()) > 100.0)
					ftfVizeNotu.setText("100.00");
				if(Double.parseDouble(ftfProjeNotu.getText()) > 100.0)
					ftfProjeNotu.setText("100.00");
				if(Double.parseDouble(ftfFinalNotu.getText()) > 100.0)
					ftfFinalNotu.setText("100.00");
				
				nOgrenci.setIsim(tfOgrenciAdi.getText());
				nOgrenci.setOgrenciNo(Integer.parseInt(ftfOgrenciNo.getText()));
				nOgrenci.setVizeNotu(Double.parseDouble(ftfVizeNotu.getText()));
				nOgrenci.setProjeNotu(Double.parseDouble(ftfProjeNotu.getText()));
				nOgrenci.setFinalNotu(Double.parseDouble(ftfFinalNotu.getText()));
				lblGenelNotDegeri.setText(nOgrenci.notHesapla());
				lblHarfNotuDegeri.setText(nOgrenci.harfNotuCevir());
			}
		});
		
		JButton btnYeniOgrenci = new JButton("Yeni Ogrenci");
		contentPane.add(btnYeniOgrenci, "16, 10, 5, 1");
		btnYeniOgrenci.addActionListener(new ActionListener()
		{
			
			public void actionPerformed(ActionEvent arg0)
			{
				nOgrenci.setIsim("");
				nOgrenci.setOgrenciNo(0);
				nOgrenci.setVizeNotu(0);
				nOgrenci.setProjeNotu(0);
				nOgrenci.setFinalNotu(0);
				tfOgrenciAdi.setText("");
				ftfOgrenciNo.setText("");
				ftfVizeNotu.setText("000.00");
				ftfProjeNotu.setText("000.00");
				ftfFinalNotu.setText("000.00");
				lblGenelNotDegeri.setText("");
				lblHarfNotuDegeri.setText("");
			}
		});
	}

}

class Ogrenci
{
	private String isim;
	private int ogrenciNo;
	private double vizeNotu, projeNotu, finalNotu;
	
	public String getIsim()
	{
		return isim;
	}
	
	public void setIsim(String isim)
	{
		this.isim = isim;
	}
	
	public int getOgrenciNo()
	{
		return ogrenciNo;
	}
	
	public void setOgrenciNo(int ogrenciNo)
	{
		this.ogrenciNo = ogrenciNo;
	}
	
	public double getVizeNotu()
	{
		return vizeNotu;
	}
	
	public void setVizeNotu(double vizeNotu)
	{
		this.vizeNotu = vizeNotu;
	}
	
	public double getProjeNotu()
	{
		return projeNotu;
	}
	
	public void setProjeNotu(double projeNotu)
	{
		this.projeNotu = projeNotu;
	}
	
	public double getFinalNotu()
	{
		return finalNotu;
	}
	
	public void setFinalNotu(double finalNotu)
	{
		this.finalNotu = finalNotu;
	}
	
	public String notHesapla()
	{
		double sonuc = 0;
		
		sonuc += this.finalNotu * 40.0  / 100.0;
		sonuc += this.projeNotu * 35.0  / 100.0;
		sonuc += this.vizeNotu * 25.0  / 100.0;
		
		return Integer.toString((int)sonuc);
	}
	
	public String harfNotuCevir()
	{
		byte not = Byte.parseByte(this.notHesapla()); 
		if(not < 90)
			if(not < 85)
				if(not < 75)
					if(not < 70)
						if(not < 60)
							if(not < 55)
								if(not < 50)
									return "FF";
								else
									return "DD";
							else
								return "DC";
						else
							return "CC";
					else
						return "CB";
				else
					return "BB";
			else
				return "BA";
		else
			return "AA";
	}
}